<template>
  <div>
    <div>
      <el-input
          size="small"
          style="width: 300px"
          placeholder="添加职称"
          prefix-icon="el-icon-plus"
          v-model="jl.name">
      </el-input>
      <el-select v-model="jl.titlelevel" placeholder="职称等级" size="small" style="margin-left: 8px;margin-right: 8px">
        <el-option
            v-for="item in titlelevels"
            :key="item"
            :label="item"
            :value="item">
        </el-option>
      </el-select>
      <el-button icon="el-icon-plus" size="small" type="primary" @click="addJobLevel" >添加</el-button>
    </div>
    <div>
      <el-table
          :data="joblevel"
          border
          stripe
          size="small"
          @selection-change="handleSelectionChange"
          style="width: 70% ">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称名称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="titlelevel"
            label="职称等级"
            width="180">
        </el-table-column>
        <el-table-column
            prop="createdate"
            width="180"
            label="创建时间">
        </el-table-column>
        <el-table-column label="是否启用">
          <template slot-scope="scope">
            <el-tag size="small" type="success" v-if="scope.row.enabled">已启用</el-tag>
            <el-tag size="small" type="danger" v-else>未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="showEditView(scope.$index,scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="danger" size="small" style="margin-top: 10px" :disabled="multipleSelection.length==0"
                 @click="deleteMany">批量删除
      </el-button>
    </div>
    <el-dialog
        title="修改职称"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <table>
          <tr>
            <td>
              <el-tag>职称名</el-tag>
            </td>
            <td>
              <el-input  size="small" v-model="updatejl.name"></el-input>
            </td>
          </tr>
          <tr style="margin-top: 8px;margin-bottom: 8px">
            <td><el-tag>职称级别</el-tag></td>
            <td>
              <el-select v-model="updatejl.titlelevel" placeholder="职称等级" size="small" style="margin-left: 8px;margin-right: 8px">
                <el-option
                    v-for="item in titlelevels"
                    :key="item"
                    :label="item"
                    :value="item">
                </el-option>
              </el-select>
            </td>
          </tr>
          <tr>
            <td><el-tag>是否启用</el-tag></td>
            <td>
              <el-switch
                  v-model="updatejl.enabled"
                  active-text="启用"
                  inactive-text="禁用">
              </el-switch>
            </td>
          </tr>
        </table>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="doUpdate">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {getRequest} from "../../../utils/api";
import {postRequest} from "../../../utils/api";
import {putRequest} from "../../../utils/api";
import {deleteRequest} from "../../../utils/api";

export default {
  name: "JobLevelMana",
  data(){
    return{
      updatejl:{
        name:'',
        titlelevel: '',
        enabled:'',
      },
      dialogVisible:false,
      joblevel:[],
      jl:{
        name:'',
        titlelevel:'',
      },
      multipleSelection:[],
      titlelevels:[
        '正高级',
        '副高级',
        '中级',
        '初级',
        '员级',
      ],
    }
  },
  mounted() {
    this.initJobLevel();
  },
  methods:{
    doUpdate(){
      putRequest("/system/basic/jl/",this.updatejl).then((res)=>{
        if(res){
          this.initJobLevel();
          this.updatejl={};
          this.dialogVisible=false;
        }
      })
    },
    showEditView(index,data){
      Object.assign(this.updatejl,data);
      this.dialogVisible = true;
    },
    deleteMany(){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?'
        this.multipleSelection.forEach((item)=>{
          ids += "ids="+item.id+"&";
        })
        deleteRequest("/system/basic/jl/"+ids).then((res)=>{
          if(res){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.initJobLevel();
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addJobLevel(){
      if(this.jl.name ){
        postRequest("/system/basic/jl/",this.jl).then((res)=>{
          if(res){
            this.jl.name='';
            this.jl.titleLevel = '';
            this.initJobLevel();
          }
        })
      }
    },
    initJobLevel(){
      getRequest("/system/basic/jl/").then((res)=>{
        if(res){
          this.joblevel = res;
        }
      });
    },
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    handleDelete(index,data){
      this.$confirm('此操作将永久删除【'+data.name+'】职位，是否继续？','提示',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:'warning'
      }).then(()=>{
        deleteRequest("/system/basic/jl/"+data.id).then((res)=>{
          if (res){
            this.initJobLevel();
          }
        })
      }).catch(()=>{
        this.$message({
          type:'info',
          message:'已取消删除'
        })
      })
    },

  }
}
</script>

<style scoped>

</style>